package com.ybt.service.impl.pack;

import java.io.ByteArrayOutputStream;

import com.ruleengine.context.IContextProperty;
import com.ruleengine.context.IWorkflowConext;
import com.ruleengine.endpoint.data.IEndpointData;
import com.ybt.context.IYBTContextConstants;
import com.ybt.service.exception.ServiceException;
import com.ybt.service.impl.BlockingBaseService;

public class CcbPack extends BlockingBaseService {

    @Override
    public void service(IWorkflowConext context, IEndpointData data)
            throws ServiceException {
        // TODO Auto-generated method stub
        try {

            IContextProperty op = context
                    .getContextProperty(IYBTContextConstants.ORIGINAL_DATA);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            String body = (String) data.getOutData();
            byte[] bodyarr = body.getBytes("UTF-8");

            String length = bodyarr.length + "";
            while (length.length() < 4) {
                length = "0" + length;
            }
            byte[] lengtharr = length.getBytes("UTF-8");

            // 外部交易码
            IContextProperty<String> funp = context
                    .getContextProperty(IYBTContextConstants.OUT_TRANCODE);
            byte[] funarr = funp.getValue().getBytes("UTF-8");

            out.write(lengtharr);
            out.write(funarr);
            out.write(bodyarr);

            op.setValue(out.toByteArray());

        } catch (Exception e) {
            throw new ServiceException(e);
        }

    }

}
